package asu.util
{
	public class StringUtil
	{
		public static var TIME_DEFAULT	:String = "00:00";
		public static var TIME_DELIM	:String = ":";
		
		/**
		 * Generate a Random String
		 * 
		 * Borrowed from http://www.zedia.net/2008/generate-random-strings-in-as2-or-as3/ ;) 
		 * @param newLength
		 * @param userAlphabet
		 * @return 
		 * 
		 */		
		public static function generateRandomString(newLength:uint = 1, userAlphabet:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"):String
		{
			var alphabet:Array = userAlphabet.split("");
			var alphabetLength:int = alphabet.length;
			var randomLetters:String = "";
			for (var i:uint = 0; i < newLength; i++)
			{
				randomLetters += alphabet[int(Math.floor(Math.random() * alphabetLength))];
			}
			return randomLetters;
		}
		
		
		public static function replace( string:String, find:Array, replace:Array ):String
		{
			var c:uint = find.length;
			
			if( replace.length != c ) return string;
			
			for( var i:uint = 0; i < c; i++ )
			{
				string = string.replace( find[i], replace[i] );
			}
			
			return string;
		}
		
		public static function replaceVars( str:String, obj:Object, delim:String="%" ):String
		{
			for( var i:String in obj )
				str = str.replace( new RegExp( "\\"+delim+i+"\\"+delim, 'g' ), String(obj[i]) );
			
			return str;
		}
		
		public static function getTimeFormattedString( value:Number ):String
		{
			if( !value || isNaN( value ) )
			{
				return TIME_DEFAULT;
			}
			
			var minutes:int = Math.floor( value/60 );
			var minStr:String = (minutes < 10)? "0" + minutes.toString() : minutes.toString();
			var sec:int = value - minutes*60;
			var secStr:String = (sec < 10)? "0" + sec.toString() : sec.toString();
			return minStr + TIME_DELIM + secStr;
		}
		
		public static function capitalize(str:String):String 
		{
			var firstChar:String = str.substr(0, 1);
			var restOfString:String = str.substr(1, str.length);
			return firstChar.toUpperCase()+restOfString.toLowerCase();
		}
		
		public static function removeWhiteSpace(str:String):String 
		{
			return str.split(" ").join("");
		}
		
		public static function camelCase(str:String):String 
		{
			var words:Array = str.split(" ");
			var newstring:String = '';
			
			for each( var word:String in words )
			{
				newstring += capitalize( word );
			}
			
			return newstring;
		}
	}
}